from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from database.database import get_db
from core.base.api_mode import ApiResponse, api_success
from modules.system.schemas.dict_item_schemas import DictItemCreate, DictItemUpdate
from modules.system.params.dict_item_params import DictItemQuery
from modules.system.services.dict_item_service import DictItemService
from modules.system.vos.dict_item_vo import DictItemResponse, DictItemListResponse
from core.pagination import DependsPagination

router = APIRouter()


@router.post("/create", response_model=ApiResponse[DictItemResponse], summary="创建字典项")
async def create_dict_item(payload: DictItemCreate, db: AsyncSession = Depends(get_db)):
    item = await DictItemService.create_item(db, payload)
    return api_success(DictItemResponse.model_validate(item), msg="创建成功")


@router.post(
    "/list", 
    response_model=ApiResponse[DictItemListResponse], 
    summary="获取字典项列表",
    dependencies=[DependsPagination]
)
async def list_dict_items(query_params: DictItemQuery, db: AsyncSession = Depends(get_db)):
    result = await DictItemService.get_items_with_pagination(db, query_params)
    return api_success(data=result, msg="获取成功")


@router.put("/{item_id}", response_model=ApiResponse[DictItemResponse], summary="更新字典项")
async def update_dict_item(item_id: int, payload: DictItemUpdate, db: AsyncSession = Depends(get_db)):
    item = await DictItemService.update_item(db, item_id, payload)
    return api_success(DictItemResponse.model_validate(item), msg="更新成功")


@router.delete("/{item_id}", response_model=ApiResponse[dict], summary="删除字典项")
async def delete_dict_item(item_id: int, db: AsyncSession = Depends(get_db)):
    await DictItemService.delete_item(db, item_id)
    return api_success({"message": f"字典项ID {item_id} 删除成功"}, msg="删除成功")